package weibo.common.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import weibo.common.result.JsonResult;
import weibo.common.result.StatusCode;
import weibo.pojo.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GheckLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        // 如果用户没有登录，则跳转到登录页面
        if (user==null){
            response.sendRedirect("/login.html");
            response.getWriter().write(new JsonResult(StatusCode.NOT_LOGIN).toString());
            return false;
        }else {
            return true;
        }

    }
}
